package at.codebase.core.search;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import at.codebase.core.StringTools;
import at.codebase.security.SecureStringBuilder;

/**
 * @author Daniel Marth <danielmarth@gmx.at>
 */
public class ItemSearchQuery {

	private List<SearchTerm> terms = new ArrayList<SearchTerm>();

	public ItemSearchQuery(final String line) {
		terms = StringTools.buildItemSearchQuery(line);
	}

	public String compile() {
		SecureStringBuilder builder = new SecureStringBuilder();
		boolean firstTerm = true;
		builder.appendInsecure("(");
		for (SearchTerm it : terms) {
			if (!firstTerm) {
				builder.appendInsecure(" OR ");
			}
			firstTerm = false;
			builder.appendInsecure(it.getTerm());
		}
		builder.appendInsecure(") AND ");
		builder.appendInsecure("(timeExpiration > ");
		builder.appendInsecure(String.valueOf(new Date().getTime() / 1000));
		builder.appendInsecure(")");
		return builder.toString();
	}
}
